package org.xmap.ui.control;

import javax.microedition.lcdui.Graphics;

import org.xmap.ui.base.Window;

public final class Guage extends Window {

    public static final int NONINTERACT_MODE = 0;
    public static final int INTERACT_MODE = 1;
    private int mode = Guage.NONINTERACT_MODE;
    private int guageColor = 0;
    private int value;
    private int marginX = 2;
    private int marginY = 3;
    private int disWidth;

    public Guage(int leftUpX, int leftUpY, int width, int height, int orient) {
        super(leftUpX, leftUpY, width, height, orient);
        this.disWidth = width / 10;
        this.setSetClipBeforeDraw(true);
    }

    public void setGuageColor(int color) {
        this.guageColor = color;
    }

    public void setValue(int value) {
        if (this.mode == Guage.INTERACT_MODE) {
            this.value = value;
        }
    }

    public int getValue() {
        return this.value;
    }

    public void setMargin(int marginX, int marginY) {
        if (marginX > 0 && marginX < this.getRect().width >> 1) {
            this.marginX = marginX;
        }
        if (marginY > 0 && marginY < this.getRect().height >> 1) {
            this.marginY = marginY;
        }
    }

    public int getMargin() {
        return this.marginX;
    }

    public void update() {
        if (this.mode == Guage.NONINTERACT_MODE) {
            this.value = ((this.value + 4) % 110);
        }

    }

    public void draw(Graphics g) {
        switch (this.mode) {
        case Guage.NONINTERACT_MODE:
            this.disWidth = this.getRect().width >> 3;
            g.setColor(super.bgColor);
            g.fillRect(0, 0, this.getRect().width, this.getRect().height);
            g.setColor(this.guageColor);
            g.translate(this.marginX - this.disWidth, this.marginY);
            g.fillRect(this.value * this.getRect().width / 100, 0,
                       this.disWidth, this.getRect().height - (this.marginY << 1));
            g.translate( -(this.marginX - this.disWidth), -this.marginY);
            break;
        case Guage.INTERACT_MODE:
            g.setColor(super.bgColor);
            g.fillRect(0, 0, this.getRect().width, this.getRect().height);
            g.setColor(this.guageColor);
            g.fillRect(this.marginX, this.marginY,
                       (this.getRect().width - (this.marginX << 1)) * this.value / 100,
                       this.getRect().height - (this.marginY << 1));
            break;
        }
    }
}
